/**
 * 作者：陈文斌 on 2018/12/28.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'package:crm/view/Dialog/captcha_dialog.dart';
import 'package:crm/page/SettingPassword/crm_setting_gesture_password_page.dart';
import 'package:crm/tools/shared_preferences_util.dart';
import 'package:crm/tools/string_util.dart';
import 'package:crm/viewModel/Login/login_view_model.dart';
import 'package:crm/component/Login/login_form_code.dart';
import 'package:crm/view/AppBar/custom_app_bar.dart';

const String Title = '忘记密码';

class ForgetPasswordPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _ForgetPasswordPageState();
}

class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
  String _verificationCode = '';
  String _phoneNumber;

  String _hintText;

  @override
  void initState() {
    super.initState();
    _phoneNumber = StringUtil.changePhoneNumberFormat('17805918554');
    _hintText = '验证码发送到您的手机$_phoneNumber';
  }

  @override
  Widget build(BuildContext context) {
    // 输入框管理器
    TextEditingController _verificationCodeController =
        TextEditingController.fromValue(
      TextEditingValue(
        text: _verificationCode,
        selection: TextSelection.fromPosition(
          TextPosition(
              affinity: TextAffinity.downstream,
              offset: _verificationCode.length),
        ),
      ),
    );

    Widget _promptTextSection() {
      return Container(
        width: AppScreen.setWidth(300),
        margin: EdgeInsets.fromLTRB(
          AppScreen.setWidth(37),
          36,
          AppScreen.setWidth(38),
          0,
        ),
        child: Text(
          _hintText,
          style: TextStyle(
            color: AppColors.TextColor,
            fontSize: AppFontSizes.Font15,
            fontFamily: AppFontFamilies.PingFangSC,
            fontWeight: FontWeight.w500,
          ),
        ),
      );
    }

    Widget _verificationCodeSection() {
      return Container(
        width: AppScreen.setWidth(300),
        height: 37,
        margin: EdgeInsets.fromLTRB(
          AppScreen.setWidth(37),
          36,
          AppScreen.setWidth(38),
          0,
        ),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: AppColors.LineColor,
              width: 1,
            ),
          ),
        ),
        child: Row(
          children: <Widget>[
            Expanded(
              child: TextField(
                controller: _verificationCodeController,
                textAlign: TextAlign.start,
                style: TextStyle(
                  color: AppColors.TextColor,
                  fontSize: AppFontSizes.Font15,
                  fontFamily: AppFontFamilies.PingFangSC,
                  fontWeight: FontWeight.w500,
                ),
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.only(top: 0),
                  hintText: '请输入验证码',
                  border: InputBorder.none,
                  hintStyle: TextStyle(
                    color: AppColors.PlaceholderTextColor,
                    fontSize: AppFontSizes.Font15,
                    fontFamily: AppFontFamilies.PingFangSC,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                keyboardType: TextInputType.number,
                textInputAction: TextInputAction.done,
                onChanged: (str) {
                  _verificationCode = str;
                  setState(() {});
                },
                onEditingComplete: () {
                  FocusScope.of(context).requestFocus(FocusNode());
                },
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 5),
            ),
            LoginFormCode(),
          ],
        ),
      );
    }

    Widget _buttonSection() {
      Color _defaultColor = Color(0xFF9AD1FF);
      Color _highlightColor = Color(0xFF3C86F3);
      bool _isHighlight = _verificationCode.length > 0;

      return Container(
        width: AppScreen.setWidth(300),
        height: 42,
        margin: EdgeInsets.only(
          left: AppScreen.setWidth(36),
          top: AppScreen.setHeight(130),
          right: AppScreen.setWidth(36),
        ),
        decoration: BoxDecoration(
          color: !_isHighlight ? _defaultColor : _highlightColor,
          borderRadius: BorderRadius.circular(21),
        ),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(21),
          child: MaterialButton(
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => SettingGesturePasswordPage(
                    appBarTitle: '重置手势密码',
                  ),
                ),
              );
            },
            child: Text(
              '下一步',
              style: TextStyle(
                fontSize: AppFontSizes.Font15,
                fontFamily: AppFontFamilies.PingFangSC,
                fontWeight: FontWeight.w500,
              ),
            ),
            textColor: Colors.white,
          ),
        ),
      );
    }

    return Scaffold(
      backgroundColor: Colors.white,
      resizeToAvoidBottomInset: false,
      appBar: CustomAppBar(
        title: Title,
      ),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Column(
          children: <Widget>[
            _promptTextSection(),
            _verificationCodeSection(),
            _buttonSection(),
          ],
        ),
      ),
    );
  }
}
